<?php 
//all the class/function includes are managed in the includer file
include("include/includer.php");

//get all the configuration variables (custom function in newzbook.php include)
getConfigVars(true);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<HEAD>
    <?php standardHead("Debug"); ?>
    <link type="text/css" href="style/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />	
		<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
</HEAD>

<BODY>
<?php internetSecurity(); ?>
<div style="visibility:hidden" id="progress"><div id="progress_bg"></div><a name="loading" /><div id="progress_box"><img id="progress_image" style="padding-left:5px;padding-top:5px;" src="images/ajax-loader.gif" alt="loading...">You request is being processed...</div></div>
<div class="headercontent">
  <?php headerContent(); ?>
</div>
<div class="maincontent">
  Please send any questions or comments to <?php echo "<a href='mailto:".$cfg->get("Owner","email")."'>".$cfg->get("Owner","firstname")." ".$cfg->get("Owner","lastname")."</a>";
  if(!is_dir($config_NZBDropPath)) {
  echo "<div class='warningmessage'>The NZB Drop Path directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
}

if($config_enablenzbget == "enable"){
    if(isset($config_NZBgetAppPath) && isset($config_NZBgetConfigPath)){
      if(!is_file($config_NZBgetAppPath)) {
          echo "<div class='warningmessage'>The NZBGet Application directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
          }
      if(!is_file($config_NZBgetConfigPath)){
          echo "<div class='warningmessage'>The NZBGet Configuration directory does not appear to exist. Check your <a href='configuration.php'>configuration</a></div>";
          }
    } 
}
echo "<br/>";
?>
<script type="text/javascript">
	$(function() {
		$("#draggable").draggable({ revert: true });
		$("#droppable").droppable({
			drop: function(event, ui) {
				$("#dialog").dialog();
			}
		});

	});
	</script>
<div id="dialog" title="Secret dialog box" style="display:none;">
	Good Job! You found the secret dialog box!
</div>
<h2>Debug<img src="images/newspaper.png" id="draggable"/><img src="images/book_open.png" id="droppable"/></h2>
<script type="text/javascript">
	$(function() {
		$("#accordion").accordion({ header: 'h2', autoHeight: false });
	});
	</script>
<?php
echo "<div id='accordion'>";
    echo "<h2><a href='#'>Settings</a></h2>";
    echo "<div>";
    echo "We are using the following settings for testing: <br/>";
    echo "Application Path: $config_NZBgetAppPath [";
    if(is_file($config_NZBgetAppPath)){ echo "<font color='green'>file exists</font>";} else { echo "<font color='red'>file does not exist!</font>"; }
    echo "]<br/> Configuration Path: $config_NZBgetConfigPath [";
    if(is_file($config_NZBgetConfigPath)){ echo "<font color='green'>file exists</font>";} else { echo "<font color='red'>file does not exist!</font>"; }
    echo "]<br/> Drop Path: $config_NZBDropPath [";
    if(is_dir($config_NZBDropPath)){ echo "<font color='green'>path exists</font>";} else { echo "<font color='red'>path does not exist!</font>"; }
    echo "]<br/>";
    echo "</div>";
    
    echo "<h2><a href='#'>CSI (AppInit) Tests</a></h2>";
    echo "<div>";
    //Prepare and Run the command
    echo "/share/Apps/AppInit/appinit.cgi info<br/>";
    unset($output);
    $status = NULL;
    exec("/share/Apps/AppInit/appinit.cgi info", $output, $status);
    echo "The command returned a status of $status <br/>";
    echo "<pre>".implode("<br/>", $output)."</pre>";
    //Prepare and Run the command
    echo "/share/Apps/AppInit/appinit.cgi rescan NewzBook<br/>";
    unset($output);
    $status = NULL;
    exec("/share/Apps/AppInit/appinit.cgi rescan NewzBook", $output, $status);
    echo "The command returned a status of $status <br/>";
    echo "<pre>".implode("<br/>", $output)."</pre>";
     //Prepare and Run the command
    echo "/share/Apps/AppInit/appinit.cgi info<br/>";
    unset($output);
    $status = NULL;
    exec("/share/Apps/AppInit/appinit.cgi info", $output, $status);
    echo "The command returned a status of $status <br/>";
    echo "<pre>".implode("<br/>", $output)."</pre>";
    echo "<br/>";
    echo "</div>";
    
    echo "<h2><a href='#'>Cron Job (Timed Job)</a></h2>";
    echo "<div>";
    echo "The cron file will only show up if the cron entry is actually running properly:<br/>";
    echo "<pre>".file_get_contents("../logs/cron/cron.txt")."</pre>";
    //echo "I've disabled this for the time being.<br/>";
   
    unset($output);
    echo "Let's try to run the crontab -l system command (for root): <br/>";
    echo exec("crontab -u root -l", $output);
    //print_r($output);
    echo "<pre>".implode("<br/>",$output)."</pre>";
    echo "Let's see how long it's been since the log was written to...<br/>";
    unset($output);
    echo "Newest file in the logs folder is: $bookmarklogs<br/>";
    define('YOUR_EOL', "\n");
    $fp = fopen($bookmarklogs, 'r');
    $pos = -2; $line = ''; $c = '';
    do {
        $line = $c . $line;
        fseek($fp, $pos--, SEEK_END);
        $c = fgetc($fp);
    } while ($c != YOUR_EOL);
    echo "The last line of the file was: <pre>$line</pre>";
    fclose($fp);
    preg_match("/\(([^\:]+)\) \(([^\-]+)\)/",$line,$matches);
    //echo "Log Date: $matches[1] | Log Time: $matches[2] <br/>";
    echo "The current 'log' date/time is (".date("Y-m-d").") (".date("g:i a")."). Note: this time may differ from your local time.<br/>";
    echo "30 minutes ago: ".strtotime("-30 minutes")." | Log Time: ".strtotime($matches[1]." ".$matches[2])."<br/>";
    if(strtotime($matches[1]." ".$matches[2]) >= strtotime("-30 minutes")){ echo "<font color='green'>The job appears to have successfully run within the allotted timeframe (30 minutes)</font><br/>"; } else { echo "<div class='warningmessage'>There appears to be an issue with the cron job running on time</div>"; }
    echo "<br/>Let's try to run the crontab.sh script for NewzBook: <br/>";
    echo "<br/>Let's try to run the crontab.sh script for NewzBook: <br/>";
    exec("../crontab.sh",$output);
    //print_r($output);
    echo "<pre>".implode("<br/>",$output)."</pre>";
    echo "</div>";
    
    echo "<h2><a href='#'>NZBget Configuration</a></h2>";
    echo "<div>";
    //clear the array
    unset($output);
    //get the array of groups (NZBs) that are queued (includes junk text)
    exec("$config_NZBgetAppPath -p -c $config_NZBgetConfigPath", $output);
    //unset($output[0]);
    echo "<pre>".implode("<br/>", $output)."</pre>";
    echo "<br/>";
    echo "</div>";
    
    echo "<h2><a href='#'>List Groups</a></h2>";
    echo "<div>";
    //clear the array
    unset($output);
    //get the array of groups (NZBs) that are queued (includes junk text)
    exec("$config_NZBgetAppPath -L G -c $config_NZBgetConfigPath", $output);
    //unset($output[0]);
    echo "<pre>".implode("<br/>", $output)."</pre>";
    echo "<br/>";
    echo "</div>";
    
    echo "<h2><a href='#'>NZBGet List Generic (files)</a></h2>";
    echo "<div>";
    //clear the array
    unset($output);
    //get the array of groups (NZBs) that are queued (includes junk text)
    exec("$config_NZBgetAppPath -L -c $config_NZBgetConfigPath", $output);
    //unset($output[0]);
    echo "<pre>".implode("<br/>", $output)."</pre>";
    echo "<br/>";
    echo "</div>";
    
    echo "<h2><a href='#'>NZBGet List Status</a></h2>";
    echo "<div>";
    //clear the array
    unset($output);
    //get the array of groups (NZBs) that are queued (includes junk text)
    exec("$config_NZBgetAppPath -L S -c $config_NZBgetConfigPath", $output);
    //unset($output[0]);
    echo "<pre>".implode("<br/>", $output)."</pre>";
    echo "<br/>";
    echo "</div>";
    
    echo "<h2><a href='#'>NZBGet Version</a></h2>";
    echo "<div>";
    //clear the array
    unset($output);
    //get the array of groups (NZBs) that are queued (includes junk text)
    exec("$config_NZBgetAppPath -V -c $config_NZBgetConfigPath", $output);
    //unset($output[0]);
    echo "<pre>".implode("<br/>", $output)."</pre>";
    echo "<br/>";    
    echo "</div>";
    ?>
  </div>
</div>
</BODY>
</HTML>